package com.acce.product.jpush;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.acce.product.config.HeaderType;
import com.acce.product.exception.ApplicationException;
import com.acce.product.pojo.CommonHeader;
import com.alibaba.fastjson.JSONObject;

public class PushMessage implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Logger logger = Logger.getLogger(PushMessage.class);
	
	private String alert;

	private String title;
	
	private String msgContent;
	
	private String biuldId;
	
	private String contentType;
	
	private JSONObject extras;
	
	private List<String> tag = new ArrayList<String>();
	
	private List<String> tagEnd = new ArrayList<String>();
	
	private List<String> alias = new ArrayList<String>();
	
	private List<String> registrationId = new ArrayList<String>();
	
	private CommonHeader header;

	public PushMessage(CommonHeader header) throws ApplicationException {
		if(header == null || StringUtils.isEmpty(header.getHeaderType())) {
			logger.debug("init push message body failure.");
			throw new ApplicationException("init push message body failure.");
		}
		this.header = header;
		
		buildMessage();
	}
	
	private void buildMessage() {
		if(HeaderType.purchase.getType().equals(header.getHeaderType())) {
			setAlert("您有一条新的申购单信息");
			setTitle("申购单【"+header.getPurchaseId()+"】");
			setMsgContent(getTitle());
			tag.add(header.getSendCompanyId());
			alias.add(header.getSendUserId());
		}
		if(HeaderType.inquiry.getType().equals(header.getHeaderType())) {
			setAlert("您有一条新的询价单信息");
			setTitle("询价单【"+header.getPurchaseId()+"】");
			MessageCodes inquiry = new MessageCodes("2", "1", header.getPurchaseId(), "1");
			inquiry.setRecCompanyId(header.getRecCompanyId());
			inquiry.setRecUserId(header.getRecUserId());
			setMsgContent(JSONObject.toJSONString(inquiry));
			tag.add(header.getRecCompanyId());
			alias.add(header.getRecUserId());
		}
		if(HeaderType.quote.getType().equals(header.getHeaderType())) {
			setAlert("您有一条新的报价单信息");
			setTitle("报价单【"+header.getPurchaseId()+"】");
			MessageCodes quote = new MessageCodes("3", "2", header.getPurchaseId(), "1");
			quote.setRecCompanyId(header.getRecCompanyId());
			quote.setRecUserId(header.getRecUserId());
			setMsgContent(JSONObject.toJSONString(quote));
			tag.add(header.getRecCompanyId());
			alias.add(header.getRecUserId());
		}
		if(HeaderType.order.getType().equals(header.getHeaderType())) {
			setAlert("您有一条新的订单信息");
			setTitle("订单【"+header.getPurchaseId()+"】");
			MessageCodes order = new MessageCodes("2", "3", header.getPurchaseId(), "1");
			order.setRecCompanyId(header.getRecCompanyId());
			order.setRecUserId(header.getRecUserId());
			setMsgContent(JSONObject.toJSONString(order));
			tag.add(header.getRecCompanyId());
			alias.add(header.getRecUserId());
		}
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getMsgContent() {
		return msgContent;
	}

	public void setMsgContent(String msgContent) {
		this.msgContent = msgContent;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public JSONObject getExtras() {
		return extras;
	}

	public void setExtras(JSONObject extras) {
		this.extras = extras;
	}

	public String getAlert() {
		return alert;
	}

	public void setAlert(String alert) {
		this.alert = alert;
	}

	public String getBiuldId() {
		return biuldId;
	}

	public void setBiuldId(String biuldId) {
		this.biuldId = biuldId;
	}

	public List<String> getTag() {
		return tag;
	}

	public void setTag(List<String> tag) {
		this.tag = tag;
	}

	public List<String> getAlias() {
		return alias;
	}

	public void setAlias(List<String> alias) {
		this.alias = alias;
	}

	public List<String> getTagEnd() {
		return tagEnd;
	}

	public void setTagEnd(List<String> tagEnd) {
		this.tagEnd = tagEnd;
	}

	public List<String> getRegistrationId() {
		return registrationId;
	}

	public void setRegistrationId(List<String> registrationId) {
		this.registrationId = registrationId;
	}
}
